package zmee.common.allhelper;

import com.kingdee.bos.qing.util.MapUtils;
import com.kingdee.bos.util.backport.Arrays;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.pmgt.pmbs.common.enums.BudgetSourceTypeEnum;
import zmee.entity.BudgetProInfo;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yss
 * @createTime 2022-12-02 10:54
 * @description 已占用金额计算
 */
public class OccupiedAmountCalculateHelper {

    /**
     * 获取项目的所有预算金额
     * @param projectId 项目id
     * @param validType
     * @return
     */
    public static Map<Long,Map<Integer,BigDecimal>> calculateBudgetAmt(Long projectId,boolean validType,BudgetProInfo budgetProInfo){
        //1、根据项目获取项目预算项信息
        budgetProInfo = budgetProInfo != null ? budgetProInfo : new BudgetProInfo(projectId);

        Map<Long,Map<Integer,BigDecimal>> yearBudgetAmtMap = new HashMap<>();
        QFilter qFilter = new QFilter("billstatus",QCP.equals,"C");
        qFilter.and(new QFilter("project.id",QCP.equals,projectId));
        qFilter.and(new QFilter("isvalid",QCP.equals,true));
        Map<Object, DynamicObject> outBudgetMap = BusinessDataServiceHelper.loadFromCache("pmas_outbudget", new QFilter[]{qFilter});
        for(DynamicObject outBudgetDy : outBudgetMap.values()){
            //1、年度
            Date yearDate = outBudgetDy.getDate("year");
            Integer dateOfYear = getDateOfYear(yearDate);
            //2、预算 + 金额
            DynamicObjectCollection outBudgetEntryDyCo = outBudgetDy.getDynamicObjectCollection("treeentryentity");
            for(DynamicObject outBudgetEntryDy : outBudgetEntryDyCo){
                BigDecimal budgetAmount = outBudgetEntryDy.getBigDecimal("budgetamount");
                Long budgetId = outBudgetEntryDy.getLong("id");
                setBudgetAndPeriodAndAmt(dateOfYear,budgetId,budgetAmount,yearBudgetAmtMap,validType,budgetProInfo);
            }
        }
        return yearBudgetAmtMap;
    }


    /**
     * 获取项目的所有实付金额
     * @param projectId
     * @return
     */
    public static Map<Long,Map<Integer,BigDecimal>> calculateProPayAmt(Long projectId,boolean validType,BudgetProInfo budgetProInfo){
        //1、根据项目获取项目预算项信息
        budgetProInfo = budgetProInfo != null ? budgetProInfo : new BudgetProInfo(projectId);

        Map<Long,Map<Integer,BigDecimal>> yearProPayAmtMap = new HashMap<>();
        QFilter qFilter = new QFilter("billstatus",QCP.equals,"C");
        qFilter.and(new QFilter("entryentity.contpro.id",QCP.in,projectId));
        Map<Object, DynamicObject> payMentRegisterMap = BusinessDataServiceHelper.loadFromCache("pmct_paymentregister", new QFilter[]{qFilter});
        for(DynamicObject payMentRegisterDy : payMentRegisterMap.values()){
            DynamicObjectCollection entryentityDyCo = payMentRegisterDy.getDynamicObjectCollection("entryentity");
            DynamicObject periodDy = payMentRegisterDy.getDynamicObject("period");
            int periodyear = periodDy.getInt("periodyear");
            for(DynamicObject entryentityDy : entryentityDyCo){
                DynamicObject contProDy = entryentityDy.getDynamicObject("contpro");
                if(contProDy == null || projectId != contProDy.getLong("id")){
                    continue;
                }
                DynamicObjectCollection subentryentityDyCo = entryentityDy.getDynamicObjectCollection("subentryentity");
                for(DynamicObject subentryentityDy : subentryentityDyCo){
                    DynamicObject budgetitemDy = subentryentityDy.getDynamicObject("budgetitem");
                    if(budgetitemDy == null){
                        continue;
                    }
                    BigDecimal actualpayamt = subentryentityDy.getBigDecimal("actualpayamt");
                    Long budgetId = budgetitemDy.getLong("id");
                    setBudgetAndPeriodAndAmt(periodyear,budgetId,actualpayamt,yearProPayAmtMap,validType,budgetProInfo);
                }
            }
        }
        return yearProPayAmtMap;
    }

    /* ------------------------- 实付金额 end -------------------------*/

    /* ------------------------- 项目历史占用 begin -------------------------*/
    /**
     * 根据项目id获取 经营分析 -》 项目支出预算执行监控  已占用金额总和
     * @param projectPk
     * @return
     */
    public static BigDecimal getAllOccupiedByProject(Long projectPk){
        String[] outcontractSelectors = new String[]{"id contractid","openedcontract","signdate","project.id projectid","project.group","project.group.id","originaloftaxamount,originalamount"};
        //历史支出合同
        DynamicObject[] budgets = BusinessDataServiceHelper.load("pmct_outcontract", getSelectors(outcontractSelectors),
                new QFilter[]{new QFilter("project.id", QCP.equals, projectPk),new QFilter("billstatus", QCP.equals,"C")});
        //项目分类
        DynamicObject project = BusinessDataServiceHelper.loadSingle(projectPk, "bd_project");
        DynamicObject kind = BusinessDataServiceHelper.loadSingle("pmbs_projectkind","number,parent,parent.number",new QFilter("id", QCP.equals,project.getString("group.id")).toArray());

        //计算所有已占用金额 - occupyamt
        BigDecimal totalOccupyAmt = BigDecimal.ZERO;
        for (DynamicObject budget : budgets) {
            //项目分类
            if("101".equals(kind.getString("parent.number"))){
                totalOccupyAmt = totalOccupyAmt.add(budget.getBigDecimal("originaloftaxamount"));//含税合同总金额
            }else {
                totalOccupyAmt = totalOccupyAmt.add(budget.getBigDecimal("originalamount"));//不含税金额
            }
            //补充协议
            QFilter ementQFilter = new QFilter("billstatus",QCP.equals,"C");
            ementQFilter.and(new QFilter("project.id",QCP.equals,projectPk));
            ementQFilter.and(new QFilter("contract.id",QCP.equals,budget.getPkValue()));
            DynamicObject[] outaddagreement = BusinessDataServiceHelper.load("pmct_outaddagreement", "addamountoftax,addamount", ementQFilter.toArray());
            for (DynamicObject row:outaddagreement) {
                if("101".equals(kind.getString("parent.number"))){
                    totalOccupyAmt = totalOccupyAmt.add( row.getBigDecimal("addamountoftax"));//含税合同总金额
                }else {
                    totalOccupyAmt = totalOccupyAmt.add(row.getBigDecimal("addamount"));//不含税金额
                }
            }
        }
        return totalOccupyAmt;
    }

    /**
     * 获取历史年度占用金额
     * 项目历史年度占用金额=开口合同金额+非开口合同金额。
     * @param projectId 项目id
     * @return
     */
    public static BigDecimal calculateBeforeThisDateOccupiedAmountCalculate(Long projectId,Date thisDate){
        thisDate = thisDate != null ? thisDate : new Date();
        Calendar thisDateCalendar = Calendar.getInstance();
        thisDateCalendar.setTime(thisDate);
        BigDecimal beforeThisDateOccupiedAmount = BigDecimal.ZERO;
        Map<Integer, BigDecimal> allOccupiedAmountMap = calculateOccupiedAmountCalculate(projectId);
        for(Map.Entry<Integer, BigDecimal> allOccupiedAmountEntry : allOccupiedAmountMap.entrySet()){
            if(thisDateCalendar.get(Calendar.YEAR) != allOccupiedAmountEntry.getKey()){
                beforeThisDateOccupiedAmount = beforeThisDateOccupiedAmount.add(allOccupiedAmountEntry.getValue());
            }
        }
        return beforeThisDateOccupiedAmount;
    }


    /**
     * 获取历史年度占用金额
     * 项目历史年度占用金额=开口合同金额+非开口合同金额。
     * @param projectId 项目id
     * @return
     */
    public static Map<Integer,BigDecimal> calculateOccupiedAmountCalculate(Long projectId){
        Map<Integer,BigDecimal> occupiedAmountMap = new HashMap();
        //项目历史年度占用金额=开口合同金额+非开口合同金额。
        //根据项目获取支出合同信息、  select id,openedcontract(是否开口),signdate（签约日期） from pmct_outcontract where project = projectId
        String[] outcontractSelectors = new String[]{"id contractid","openedcontract","signdate","project.id projectid","project.group","project.group.id","originaloftaxamount,originalamount"};
        //支出合同
        DynamicObjectCollection outContractDyCo = QueryServiceHelper.query("pmct_outcontract", getSelectors(outcontractSelectors),
                new QFilter[]{new QFilter("project.id", QCP.equals, projectId),new QFilter("billstatus", QCP.equals,"C")});
          List<Long> outContractDyList = new ArrayList();
        for(DynamicObject outContractDy : outContractDyCo){
            long contractid = outContractDy.getLong("contractid");

            Date signDate = outContractDy.getDate("signdate");
            //获取当前年度
            Integer outContractYear = getDateOfYear(signDate);
            //项目分类
            DynamicObject kind = BusinessDataServiceHelper.loadSingle("pmbs_projectkind","number,parent,parent.number",new QFilter("id", QCP.equals,outContractDy.getString("project.group.id")).toArray());
            //合同含税总金额
            BigDecimal originaloftaxamount;
            if("101".equals(kind.getString("parent.number"))){
                originaloftaxamount = outContractDy.getBigDecimal("originaloftaxamount");
            }else {
                originaloftaxamount = outContractDy.getBigDecimal("originalamount");
            }
            outContractDyList.add(contractid);
            boolean openedContract = outContractDy.getBoolean("openedcontract");//是否开口
            //获取结算日期
            String[] outcontractSettleSelectors = new String[]{"id","project","contract","period","settleoftaxamount"};
            QFilter outContractSettleQFilter = new QFilter("project.id",QCP.equals,projectId);
            outContractSettleQFilter.and(new QFilter("contract.id",QCP.equals,contractid));
            Map<Object, DynamicObject> outContractSettleMap = BusinessDataServiceHelper.loadFromCache("pmct_outcontract_settle", getSelectors(outcontractSettleSelectors), new QFilter[]{outContractSettleQFilter});
            if(openedContract){//开口合同
                /* 开口合同金额。 */
                //取业务日期为历史年度的结算单金额，一个开口合同可能对应多个结算单，占用的年度归结算单的业务日期年度；同年度的进行相加
                //数据来源：支出合同结算 含税数据（当前结算金额） 条件：项目、计量期间
                //设置map<年度，金额>
                for(DynamicObject outContractSettleDy : outContractSettleMap.values()){
                    DynamicObject periodDy = outContractSettleDy.getDynamicObject("period");
                    if(periodDy != null){
                        int periodYear = periodDy.getInt("periodyear");
                        BigDecimal settleAmount = outContractSettleDy.getBigDecimal("settleoftaxamount");
                        setPeriodAndAmt(periodYear,settleAmount,occupiedAmountMap);
                    }
                }
            }else{//非开口日期
                /* 非开口合同金额。 */
                //判断当前合同是否结算
                if(outContractSettleMap != null && outContractSettleMap.values().size() > 0){//不为空 已结算
                    //2.1 如果合同结算，取结算金额，占用年度归合同签约日期年度，同时该合同的变更金额的占用归0；
                    //数据来源：支出合同执行决策 合同审定含税金额 条件：项目、签约日期？？？
                    //根据项目id和合同id获取支出合同执行决算单信息
                    String[] outFinalSettleSelectors = new String[]{"id","project","contract","entryentity.itemappamount"};
                    QFilter outFinalSettleQFilter = new QFilter("project.id",QCP.equals,projectId);
                    outFinalSettleQFilter.and(new QFilter("contract.id",QCP.in,outContractDyList));
                    Map<Object, DynamicObject> outFinalSettleMap = BusinessDataServiceHelper.loadFromCache("pmct_outfinalsettle", getSelectors(outFinalSettleSelectors), new QFilter[]{outFinalSettleQFilter});
                    if(outFinalSettleMap.values().size() > 0){
                        BigDecimal allFinalSettleAmount = BigDecimal.ZERO;
                        for(DynamicObject outFinalSettleValue : outFinalSettleMap.values()){
                            DynamicObjectCollection outFinalSettleEntryDyCo = outFinalSettleValue.getDynamicObjectCollection("entryentity");
                            for(DynamicObject outFinalSettleEntryDy : outFinalSettleEntryDyCo){
                                BigDecimal itemappamount = outFinalSettleEntryDy.getBigDecimal("itemappamount");
                                allFinalSettleAmount = allFinalSettleAmount.add(itemappamount);
                            }
                        }
                        setPeriodAndAmt(outContractYear,allFinalSettleAmount,occupiedAmountMap);
                    }
                }else {//为空，未结算
                    setPeriodAndAmt(outContractYear,originaloftaxamount,occupiedAmountMap);
                    //2.2 如果合同没有结算，取合签约金额，占用年度归合同签约日期年度；如果合同有签证，签证金额占用归签证单业务日期年度。
                    //判断是否存在签证
                    String[] outcontractVisionSelectors = new String[]{"id","project.id projectid","contract.id contractid","period","period.periodyear","revisionoftaxamount"};
                    QFilter outContractVisionQFilter = new QFilter("project.id",QCP.equals,projectId);
                    outContractVisionQFilter.and(new QFilter("contract.id",QCP.in,outContractDyList));
                    Map<Object, DynamicObject> outContractVisionMap = BusinessDataServiceHelper.loadFromCache("pmct_outcontractrevision",getSelectors(outcontractVisionSelectors), new QFilter[]{outContractVisionQFilter});
                    if(outContractVisionMap.values().size() != 0){
                        //2.2.2 有签证
                        for(DynamicObject outContractVisionDy : outContractVisionMap.values()){
                            DynamicObject periodDy = outContractVisionDy.getDynamicObject("period");
                            if(periodDy == null){
                                continue;
                            }
                            periodDy = BusinessDataServiceHelper.loadSingleFromCache(periodDy.getLong("id"), "bd_period", "periodyear");
                            int periodyear = periodDy.getInt("periodyear");
                            BigDecimal revisionoftaxamount = outContractVisionDy.getBigDecimal("revisionoftaxamount");
                            setPeriodAndAmt(periodyear,revisionoftaxamount,occupiedAmountMap);
                        }
                    }

                    //判断是否存在补充协议
                    QFilter outAddAgreementQFilter = new QFilter("billstatus",QCP.equals,"C");
                    outAddAgreementQFilter.and(new QFilter("project.id",QCP.equals,projectId));
                    outAddAgreementQFilter.and(new QFilter("contract.id",QCP.equals,contractid));
                    Map<Object, DynamicObject> outAddAgreementMap = BusinessDataServiceHelper.loadFromCache("pmct_outaddagreement", "addamountoftax", new QFilter[]{outAddAgreementQFilter});
                    if(outAddAgreementMap.values().size() != 0){
                        //2.2.2 有补充协议
                        for(DynamicObject outAddAgreementDy : outAddAgreementMap.values()){
                            BigDecimal addamountoftax = outAddAgreementDy.getBigDecimal("addamountoftax");
                            setPeriodAndAmt(outContractYear,addamountoftax,occupiedAmountMap);
                        }
                    }
                }
            }
        }
        return occupiedAmountMap;
    }
    /**
     * 获取项目的所有预算项的占用金额
     * @param projectId
     * @return
     */
    public static Map<Long,Map<Integer,BigDecimal>> calculateOccupiedBudgetitemAmount(Long projectId,boolean validType,BudgetProInfo budgetProInfo){
        budgetProInfo = budgetProInfo != null ? budgetProInfo : new BudgetProInfo(projectId);
        Map<Long,Map<Integer,BigDecimal>> occupiedBudgetitemMap = new HashMap<>();
        //项目历史年度占用金额=开口合同金额+非开口合同金额。
        //根据项目获取支出合同信息、  select id,openedcontract(是否开口),signdate（签约日期） from pmct_outcontract where project = projectId
        //1、根据项目获取项目预算项信息
        Map<Object, DynamicObject> outContractDyCoMap = BusinessDataServiceHelper.loadFromCache("pmct_outcontract",
                new QFilter[]{new QFilter("project.id", QCP.equals, projectId).and(new QFilter("billstatus",QCP.equals,"C"))});
        List<Long> outContractDyList = new ArrayList<>();
        for(DynamicObject outContractDy : outContractDyCoMap.values()){
            long contractid = outContractDy.getLong("id");

            Date signDate = outContractDy.getDate("signdate");
            //获取当前年度
            Integer outContractYear = getDateOfYear(signDate);
            //合同含税总金额
            BigDecimal originaloftaxamount = outContractDy.getBigDecimal("originaloftaxamount");

            outContractDyList.add(contractid);
            boolean openedContract = outContractDy.getBoolean("openedcontract");//是否开口
            //获取结算日期
            String[] outcontractSettleSelectors = new String[]{"id","project","contract","period","settleoftaxamount"};
            QFilter outContractSettleQFilter = new QFilter("project.id",QCP.equals,projectId);
            outContractSettleQFilter.and(new QFilter("contract.id",QCP.equals,contractid));
            Map<Object, DynamicObject> outContractSettleMap = BusinessDataServiceHelper.loadFromCache("pmct_outcontract_settle", getSelectors(outcontractSettleSelectors), new QFilter[]{outContractSettleQFilter});
            if(!openedContract){//非开口日期
                /* 非开口合同金额。 */
                //判断当前合同是否结算
                if(outContractSettleMap != null && outContractSettleMap.values().size() > 0){
                    //不为空 已结算
                    //2.1 如果合同结算，取结算金额，占用年度归合同签约日期年度，同时该合同的变更金额的占用归0；
                    //数据来源：支出合同执行决策 合同审定含税金额 条件：项目、签约日期？？？
                    //根据项目id和合同id获取支出合同执行决算单信息
                    String[] outFinalSettleSelectors = new String[]{"id","project","contract","entryentity.itemappamount","entryentity.budgetitem"};
                    QFilter outFinalSettleQFilter = new QFilter("project.id",QCP.equals,projectId);
                    outFinalSettleQFilter.and(new QFilter("contract.id",QCP.in,outContractDyList));
                    Map<Object, DynamicObject> outFinalSettleMap = BusinessDataServiceHelper.loadFromCache("pmct_outfinalsettle", getSelectors(outFinalSettleSelectors), new QFilter[]{outFinalSettleQFilter});
                    if(outFinalSettleMap.values().size() > 0){
                        BigDecimal allFinalSettleAmount = BigDecimal.ZERO;
                        for(DynamicObject outFinalSettleValue : outFinalSettleMap.values()){
                            DynamicObjectCollection outFinalSettleEntryDyCo = outFinalSettleValue.getDynamicObjectCollection("entryentity");
                            for(DynamicObject outFinalSettleEntryDy : outFinalSettleEntryDyCo){
                                BigDecimal itemappamount = outFinalSettleEntryDy.getBigDecimal("itemappamount");
                                DynamicObject budgetItemDy = outFinalSettleEntryDy.getDynamicObject("budgetitem");
                                if(budgetItemDy != null){
                                    Long budgetId = budgetItemDy.getLong("id");
                                    setBudgetAndPeriodAndAmt(outContractYear,budgetId,itemappamount,occupiedBudgetitemMap,validType,budgetProInfo);
                                }
                            }
                        }
                    }
                }else {//为空，未结算
                    //合同信息
                    DynamicObjectCollection cardEntryDyCo = outContractDy.getDynamicObjectCollection("listmodelentry");
                    for(DynamicObject cardEntryDy : cardEntryDyCo){
                        DynamicObjectCollection entryEntityDyCo = cardEntryDy.getDynamicObjectCollection("sublistentry");
                        for(DynamicObject entryEntityDy : entryEntityDyCo){
                            DynamicObject listingBudgetitemDy = entryEntityDy.getDynamicObject("listingbudgetitem");
                            BigDecimal treeAmount = entryEntityDy.getBigDecimal("oftax");
                            if(listingBudgetitemDy != null){
                                Long budgetId = listingBudgetitemDy.getLong("id");
                                setBudgetAndPeriodAndAmt(outContractYear,budgetId,treeAmount,occupiedBudgetitemMap,validType,budgetProInfo);
                            }
                        }
                    }
                    //2.2 如果合同没有结算，取合签约金额，占用年度归合同签约日期年度；如果合同有签证，签证金额占用归签证单业务日期年度。
                    //判断是否存在签证
                    String[] outcontractVisionSelectors = new String[]{"id","project.id projectid","contract.id contractid","period","period.periodyear","revisionoftaxamount","budgetitem"};
                    QFilter outContractVisionQFilter = new QFilter("project.id",QCP.equals,projectId);
                    outContractVisionQFilter.and(new QFilter("contract.id",QCP.in,outContractDyList));
                    Map<Object, DynamicObject> outContractVisionMap = BusinessDataServiceHelper.loadFromCache("pmct_outcontractrevision",getSelectors(outcontractVisionSelectors), new QFilter[]{outContractVisionQFilter});
                    if(outContractVisionMap.values().size() != 0){
                        //2.2.2 有签证
                        for(DynamicObject outContractVisionDy : outContractVisionMap.values()){
                            DynamicObject periodDy = outContractVisionDy.getDynamicObject("period");
                            if(periodDy == null){
                                continue;
                            }
                            periodDy = BusinessDataServiceHelper.loadSingleFromCache(periodDy.getLong("id"), "bd_period", "periodyear");
                            int periodyear = periodDy.getInt("periodyear");
                            BigDecimal revisionoftaxamount = outContractVisionDy.getBigDecimal("revisionoftaxamount");
                            DynamicObject budgetitemDy = outContractVisionDy.getDynamicObject("budgetitem");
                            if(budgetitemDy != null){
                                Long budgetId = budgetitemDy.getLong("id");
                                setBudgetAndPeriodAndAmt(periodyear,budgetId,revisionoftaxamount,occupiedBudgetitemMap,validType,budgetProInfo);
                            }
                        }
                    }

                    //判断是否存在补充协议
                    QFilter outAddAgreementQFilter = new QFilter("billstatus",QCP.equals,"C");
                    outAddAgreementQFilter.and(new QFilter("project.id",QCP.equals,projectId));
                    outAddAgreementQFilter.and(new QFilter("contract.id",QCP.equals,contractid));
                    Map<Object, DynamicObject> outAddAgreementMap = BusinessDataServiceHelper.loadFromCache("pmct_outaddagreement", "addamountoftax,budgetitem,handledate", new QFilter[]{outAddAgreementQFilter});
                    if(outAddAgreementMap.values().size() != 0){
                        //2.2.2 有补充协议
                        for(DynamicObject outAddAgreementDy : outAddAgreementMap.values()){
                            BigDecimal addamountoftax = outAddAgreementDy.getBigDecimal("addamountoftax");
                            Date handledate = outAddAgreementDy.getDate("handledate");
                            Integer handleDateYear = getDateOfYear(handledate);
                            DynamicObject budgetitemDy = outAddAgreementDy.getDynamicObject("budgetitem");
                            if(budgetitemDy != null){
                                Long budgetId = budgetitemDy.getLong("id");
                                setBudgetAndPeriodAndAmt(handleDateYear,budgetId,addamountoftax,occupiedBudgetitemMap,validType,budgetProInfo);
                            }
                        }
                    }
                }
            }
        }
        return occupiedBudgetitemMap;
    }


    /**
     * 设置Map<Long,Map<Integer,BigDecimal>>型 数据
     * @param periodYear 年份
     * @param budgetId 预算id
     * @param amount 金额
     * @param occupiedBudgetitemMap 主数据
     */
    private static void setBudgetAndPeriodAndAmt(Integer periodYear,Long budgetId,BigDecimal amount,Map<Long,Map<Integer,BigDecimal>> occupiedBudgetitemMap){
        if(occupiedBudgetitemMap.containsKey(budgetId)){//已经添加了预算信息
            setPeriodAndAmt(periodYear,amount,occupiedBudgetitemMap.get(budgetId));
        }else {//未添加预算信息
            Map<Integer,BigDecimal> occupiedAmountMap = new HashMap<>();
            setPeriodAndAmt(periodYear,amount,occupiedAmountMap);
            occupiedBudgetitemMap.put(budgetId,occupiedAmountMap);
        }
    }
    private static void setBudgetAndPeriodAndAmt(Integer periodYear,Long budgetId,BigDecimal amount,Map<Long,Map<Integer,BigDecimal>> occupiedBudgetitemMap,Boolean validType,BudgetProInfo budgetProInfo){
        //根据现有id获取预算生效id
        Long budgetValidId = budgetProInfo.getBudgetValidId(budgetId, validType, periodYear);
        if(budgetValidId != 0L){
            setBudgetAndPeriodAndAmt(periodYear,budgetValidId,amount,occupiedBudgetitemMap);
        }
    }

    /**
     * 设置Map<Integer,BigDecimal>型 数据
     * @param periodYear 年份
     * @param amount 金额
     * @param occupiedAmountMap 主数据
     */
    private static void setPeriodAndAmt(Integer periodYear,BigDecimal amount,Map<Integer,BigDecimal> occupiedAmountMap){
        if(occupiedAmountMap.containsKey(periodYear)){
            occupiedAmountMap.put(periodYear,amount.add(occupiedAmountMap.get(periodYear)));
        }else {
            occupiedAmountMap.put(periodYear,amount);
        }
    }

    /**
     * 获取日期的年份
     * @param date
     * @return
     */
    public static Integer getDateOfYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date == null ? new Date() : date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 数组转字符串
     * @param selectArrays
     * @return
     */
    private static String getSelectors(String[] selectArrays){
        return String.join(",", Arrays.asList(selectArrays));
    }
    /* ------------------------- util end -------------------------*/
}
